package com.pti.usermanager.web.controller;

import com.github.pagehelper.PageInfo;
import com.pti.usermanager.dao.po.UserPo;
import com.pti.usermanager.service.api.UserService;
import com.pti.usermanager.web.dto.request.DeleteUserRequest;
import com.pti.usermanager.web.dto.request.SaveUserRequest;
import com.pti.usermanager.web.dto.request.UpdateUserRequest;
import com.pti.usermanager.web.dto.response.ResultResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName: UserController
 * @Author: liumengyao
 * @Description: 用户控制层
 * @Date: 2019/1/16
 */
@RestController
@Api(value = "用户管理接口",tags="提供用户的管理接口")
public class UserController {

    private static final Logger logger = LogManager.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @GetMapping("/user/getUserList")
    @ApiOperation(value = "获取用户列表" , notes = "获取用户列表",produces = "application/json")
    public ResultResponse getUserAll(@RequestParam(value = "page",defaultValue = "0",required = false) Integer page,
                                   @RequestParam(value = "pageSize",defaultValue = "10",required = false) Integer pageSize,
                                   @RequestParam(value = "loginNm",required = false) String loginNm,
                                   @RequestParam(value = "usrNm",required = false) String usrNm){

        logger.debug("page:{} pageSize:{} loginNm:{}",page,pageSize,loginNm);
        PageInfo<UserPo> all = userService.findAll(loginNm,usrNm, page, pageSize);
        ResultResponse resultResponse = new ResultResponse();
        resultResponse.setTotal((int)all.getTotal());
        resultResponse.setData(all.getList());
        return resultResponse;
    }

    @GetMapping("/user/getUserDetail/{usrId}")
    @ApiOperation(value = "获取用户对象" , notes = "获取用户对象",produces = "application/json")
    public ResultResponse getUserDetail(@PathVariable("usrId") Long usrId){
        logger.debug("usrId:{}",usrId);
        return  userService.findAllByUsrId(usrId);
    }

    @PostMapping("/user/saveUser")
    @ApiOperation(value = "添加用户列表" , notes = "添加用户列表",produces = "application/json")
    public ResultResponse saveUser(@RequestBody SaveUserRequest saveUserRequest){
        return userService.saveUser(saveUserRequest);
    }

    @PutMapping("/user/updateUser")
    @ApiOperation(value = "修改用户列表" , notes = "修改用户列表",produces = "application/json")
    public ResultResponse updateUser(@RequestBody UpdateUserRequest updateUserRequest){
        return userService.updateUser(updateUserRequest);
    }

    @DeleteMapping("/user/delUser")
    @ApiOperation(value = "删除用户" , notes = "删除用户",produces = "application/json")
    public ResultResponse delUset(@RequestBody DeleteUserRequest deleteUserRequest){
        return userService.delUser(deleteUserRequest);
    }


    /**
     * 跨服务调用样例-- 被调用方
     * @return
     */
    @GetMapping("/hello")
    @ApiOperation(value = "获取调用对象" , notes = "获取调用对象",produces = "application/json")
    public String hello(){
        System.out.println("访问来1了......");
        return "hello-----";
    }

}
